package com.crm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.commons.exception.BusinessException;
import com.crm.commons.exception.enumVo.BussinessExceptionEnum;
import com.crm.entity.CrmRole;
import com.crm.entity.CrmRoleMenu;
import com.crm.entity.CrmRoleUser;
import com.crm.mapper.CrmRoleMapper;
import com.crm.service.ICrmRoleMenuService;
import com.crm.service.ICrmRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.service.ICrmRoleUserService;
import com.crm.util.StringUtil;
import com.crm.vo.PageBean;
import com.crm.vo.SysRole;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author junxian, dou
 * @since 2024-11-27
 */
@Service
public class CrmRoleServiceImpl extends ServiceImpl<CrmRoleMapper, CrmRole> implements ICrmRoleService {

    @Resource
    private ICrmRoleUserService roleUserService;

    @Resource
    private ICrmRoleMenuService roleMenuService;

    @Override
    public PageBean<CrmRole> lsitPage(PageBean pageBean) {
        Map<String,String> map = pageBean.getQuery();
        String query = map.get("name").trim();
        Page<CrmRole> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        LambdaQueryWrapper<CrmRole> wrapper = new LambdaQueryWrapper<>();
        if (StringUtil.isNotEmpty(query)) {
            wrapper.eq(CrmRole::getRolename, query);
        }
        Page<CrmRole> pageResult = page(page, wrapper);

        PageBean<CrmRole> rolePageBean = new PageBean<>();
        rolePageBean.setTList(pageResult.getRecords());
        rolePageBean.setTotal(pageResult.getTotal());
        return rolePageBean;

    }

    @Override
    public void savaOrUpdateById(SysRole sysRole) {
        Integer id = sysRole.getId();
        if (id == null || id == -1) {
            save(sysRole.getRoleEntity());
        } else {
            CrmRole byId = getCrmRole(id);
            byId.setRolename(sysRole.getRolename());
            byId.setRemark(sysRole.getRemark());
            byId.setCode(sysRole.getCode());
            updateById(byId);
        }
    }

    public CrmRole getCrmRole(Integer id) {
        CrmRole byId = getById(id);
        if (ObjectUtil.isNull(byId)) {
            throw new BusinessException(BussinessExceptionEnum.ROLE_NOT_EXISTS);
        }
        return byId;
    }

    @Transactional
    @Override
    public void removeWithByIds(List<Long> list) {
        removeByIds(list);
        LambdaQueryWrapper<CrmRoleUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CrmRoleUser::getRid, list);
        roleUserService.remove(wrapper);
        LambdaQueryWrapper<CrmRoleMenu> crmRoleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        crmRoleMenuLambdaQueryWrapper.in(CrmRoleMenu::getRid, list);
        roleMenuService.remove(crmRoleMenuLambdaQueryWrapper);
    }

    @Override
    public List<Integer> hasMenusList(Integer id) {
        LambdaQueryWrapper<CrmRoleMenu> crmRoleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        crmRoleMenuLambdaQueryWrapper.eq(CrmRoleMenu::getRid, id);
        List<CrmRoleMenu> roleMenuList = roleMenuService.list(crmRoleMenuLambdaQueryWrapper);
        List<Integer> menuIdList = roleMenuList.stream().map(p -> p.getMid()).collect(Collectors.toList());

        return menuIdList;
    }

    @Transactional
    @Override
    public void updateMenus(Integer id, Integer[] menuIds) {
        LambdaQueryWrapper<CrmRoleMenu> roleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleMenuLambdaQueryWrapper.eq(CrmRoleMenu::getRid, id);
        roleMenuService.remove(roleMenuLambdaQueryWrapper);

        List<CrmRoleMenu> sysRoleMenuList = new ArrayList<>();
        Arrays.stream(menuIds).forEach(menuId -> {
            CrmRoleMenu roleMenu = new CrmRoleMenu();
            roleMenu.setRid(id);
            roleMenu.setMid(menuId);
            sysRoleMenuList.add(roleMenu);
        });
        roleMenuService.saveBatch(sysRoleMenuList);
    }
}
